package com.api.gbhg.zengping.web;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.api.gbhg.liuwenbo.utils.FormatUtil;
import com.api.gbhg.zengping.common.CommonDao;
import com.api.gbhg.zengping.common.res.Res;
import com.api.gbhg.zengping.dao.PropertyManagementCostDao;
import com.api.gbhg.zengping.dao.RetiredPeopleDao;
import com.api.gbhg.zengping.page.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: gbhg-oa
 * @author: Mr.zeng
 * @create: 2020-12-30 11:24
 *
 * 海关离退休人员工资津贴补贴经费 核批
 **/
public class RetiredPeopleAction {
    public static final Logger LOGGER = LoggerFactory.getLogger("gbhg");
    private final RetiredPeopleDao rpd=new RetiredPeopleDao();
    private final PropertyManagementCostDao rmcd=new PropertyManagementCostDao();
    private final RetiredPeopleDao getRpd=new RetiredPeopleDao();
    @POST
    @Path("/getRetiredPeople")
    @Produces(MediaType.APPLICATION_JSON)
    public String retiredPeople(Map<String,String> param){
        // 得到核批状态 3就是已核批  非3就是待核批
        String requestId = param.get("requestId");
        LOGGER.info("海关离退休人员工资津贴核批列表参数："+requestId);
        RecordSetTrans rst = rpd.getSBInfo(requestId);
        HashMap[] map=new HashMap[rst.getArray().size()];
        int i=0;
        while (rst.next()){
            map[i]=new HashMap<>();
            //序号id
            map[i].put("key",i);
            // 流程记录id
            map[i].put("lcjlsqjlid",rst.getString("lcjlsqjlid"));
            map[i].put("xmmin",rst.getString("xmmin"));
            // 申报单位
            RecordSet sbdw = rmcd.getSBDWById(rst.getInt("sbdw"));
            sbdw.next();
            map[i].put("sbdw",sbdw.getString("SUBCOMPANYNAME"));
            // 申报科室
            RecordSet sbks = rmcd.getSBKSById(rst.getInt("sbks"));
            sbks.next();
            map[i].put("sbks",sbks.getString("departmentmark"));
            //负责人
            RecordSet jbr = rmcd.getUserNameById(rst.getInt("fzr"));
            jbr.next();
            String loginid = jbr.getString("loginid");
            if(loginid==""){
                loginid="系统管理员";
            }
            map[i].put("fzr",loginid);

            map[i].put("yszje",rst.getString("yszje"));
            String mainId = rst.getString("id");
            // 得到明细表
            RecordSet rs = CommonDao.selectByDetailTable("uf_txrygzjtbtbb_dt1", mainId);
            int count = 1;
            while (rs.next()){
                //人员类型
                String rylx = rs.getString("rylx");
                String gnkm = "[30302]退休费";
                if ("离休人员".equals(rylx)){
                    count=2;
                    gnkm = "[30301]离休费";
                }
                map[i].put("gnkm"+count,gnkm);
                //离休费需求小计
                map[i].put("lxfxqxj"+count,rs.getDouble("lxfxqxj"));
                //基本离退休费需求
                map[i].put("jblxfxq"+count,rs.getDouble("jbltxfxq"));
                //住房维修金
                map[i].put("zfwxj"+count,rs.getDouble("zfwxj"));
                //计划生育奖
                map[i].put("jhsyj"+count,rs.getDouble("jhsyj"));
                //节日津贴
                map[i].put("jrjt"+count,rs.getDouble("jrjt"));
                //年终慰问金
                map[i].put("nzwwj"+count,rs.getDouble("nzwwj"));
                count=1;
            }
            i++;
        }
        PageInfo<String> pageInfo=new PageInfo();
        String tableName="uf_txrygzjtbtbb";
        RecordSetTrans sbInfoCount = getRpd.getSBInfoCount(tableName,requestId);
        sbInfoCount.next();
        int pageCount = sbInfoCount.getInt("num");
        pageInfo.setTotal(pageCount);
        pageInfo.setList(map);
        pageInfo.setFromTable(tableName);
        return JSON.toJSONString(pageInfo);
    }
    public String retiredPeople1(String requestId){
        // 得到核批状态 3就是已核批  非3就是待核批
        LOGGER.info("海关离退休人员工资津贴核批列表参数："+requestId);
        RecordSetTrans rst = rpd.getSBInfo(requestId);
        HashMap[] map=new HashMap[rst.getArray().size()];
        int i=0;
        while (rst.next()){
            map[i]=new HashMap<>();
            //序号id
            map[i].put("key",i);
            // 流程记录id
            map[i].put("lcjlsqjlid",rst.getString("lcjlsqjlid"));
            map[i].put("xmmin",rst.getString("xmmin"));
            // 申报单位
            RecordSet sbdw = rmcd.getSBDWById(rst.getInt("sbdw"));
            sbdw.next();
            map[i].put("sbdw",sbdw.getString("SUBCOMPANYNAME"));
            // 申报科室
            RecordSet sbks = rmcd.getSBKSById(rst.getInt("sbks"));
            sbks.next();
            map[i].put("sbks",sbks.getString("departmentmark"));
            //负责人
            RecordSet jbr = rmcd.getUserNameById(rst.getInt("fzr"));
            jbr.next();
            String lastname = jbr.getString("lastname");
            if(StrUtil.isEmpty(lastname)){
                lastname="系统管理员";
            }
            map[i].put("fzr",lastname);


            BigDecimal yszje = new BigDecimal(rst.getString("yszje"));
            map[i].put("yszje", yszje.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));//总金额

            String mainId = rst.getString("id");
            // 得到明细表
            RecordSet rs = CommonDao.selectByDetailTable("uf_txrygzjtbtbb_dt1", mainId);
            int count = 1;
            while (rs.next()){
                //人员类型
                String rylx = rs.getString("rylx");
                String gnkm = "[30302]退休费";
                if ("离休人员".equals(rylx)){
                    count=2;
                    gnkm = "[30301]离休费";
                }
                map[i].put("gnkm"+count,gnkm);
                //离休费需求小计
                BigDecimal lxfxqxj = new BigDecimal(rs.getString("lxfxqxj"));
                map[i].put("lxfxqxj"+count,lxfxqxj.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));

                //基本离退休费需求
                BigDecimal jblxfxq = new BigDecimal( FormatUtil.DmFormat(rs.getDouble("jbltxfxq")));
                map[i].put("jblxfxq"+count,jblxfxq.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));

                //住房维修金
                BigDecimal zfwxj = new BigDecimal(FormatUtil.DmFormat(rs.getDouble("zfwxj")));
                map[i].put("zfwxj"+count,zfwxj.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));

                //计划生育奖
                BigDecimal jhsyj = new BigDecimal(FormatUtil.DmFormat(rs.getDouble("jhsyj")));
                map[i].put("jhsyj"+count, jhsyj.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));

                //节日津贴
                BigDecimal jrjt = new BigDecimal(FormatUtil.DmFormat(rs.getDouble("jrjt")));
                map[i].put("jrjt"+count, jrjt.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));

                //年终慰问金
                BigDecimal nzwwj = new BigDecimal(FormatUtil.DmFormat(rs.getDouble("nzwwj")));
                map[i].put("nzwwj"+count, nzwwj.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));
                count=1;
            }
            i++;
        }
        PageInfo<String> pageInfo=new PageInfo();
        String tableName="uf_txrygzjtbtbb";
        RecordSetTrans sbInfoCount = getRpd.getSBInfoCount(tableName,requestId);
        sbInfoCount.next();
        int pageCount = sbInfoCount.getInt("num");
        pageInfo.setTotal(pageCount);
        pageInfo.setList(map);
        pageInfo.setFromTable(tableName);
        return JSON.toJSONString(pageInfo);
    }


    /**
     * 插入核批内容
     * @param param
     * @return
     */
    @POST
    @Path("/insertRetiredPeople")
    @Produces(MediaType.APPLICATION_JSON)
    public String insertRetiredPeople(Map<String,Object> param){
        String fromTable="formtable_main_815";
        String requestId = (String) param.get("requestId");
        param.put("requestId",requestId);
        param.put("fromTable",fromTable);
        rpd.modifyRetiredPeople(param);
        return JSON.toJSONString(Res.ok(200,""));
    }
}
